const createUUID = ((uuidRegEx, uuidReplacer) => {
  return () => {
    return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(uuidRegEx, uuidReplacer).toUpperCase();
  };
})(/[xy]/g, c => {
  let r = (Math.random() * 16) | 0;
  let v = c === 'x' ? r : (r & 3) | 8;
  return v.toString(16);
});

let windowId = '';

module.exports = {
  // 创建uuid
  createUUID,
  // 获取窗口进程id
  getThreadId() {
    if (!windowId) {
      const remote = window.require('electron').remote;
      // 在创建窗口时 会往BrowserWindow的实例上挂载一个windowId
      /**
       * @see { import('../baseWindow/main.js') }
       * @see { open }
       */
      windowId = remote && remote.getCurrentWindow().windowId;
    }
    return windowId;
  },
  isBoolean(value) {
    return !!value;
  },
  isObject(value) {
    return Object.prototype.toString.call(value) === '[object Object]';
  },
  isArray(value) {
    return Object.prototype.toString.call(value) === '[object Array]';
  },
  /**
   * 防抖
   */
  debounce(fn, wait, context) {
    let timer = null;
    return function () {
      // eslint-disable-next-line prefer-rest-params
      let args = [].slice.call(arguments);
      clearTimeout(timer);
      timer = setTimeout(() => {
        fn.apply(context, args);
      }, wait);
    };
  },
};
